from pms.common.views import PostFormula
from user.utils.create import create_user, AccountAlreadyExist


class API(PostFormula):

    require_permissions = [3]

    def handle(self, req):
        try:
            account = req['account']
            password = req['password']
            name = req['name']
            rid = req['rid']
            user_account, user_name = create_user(account, password, name, rid)
            return self.success_respond({
                'account': user_account,
                'name': user_name
            })
        except AccountAlreadyExist:
            return self.failed_respond('ACCOUNT_ALREADY_EXIST', '账号已经存在')
